package com.tanqing.ddd.web.domain.impl;

import com.tanqing.ddd.domain.domain.DomainEventListener;
import com.tanqing.ddd.domain.event.EventQueue;
import com.tanqing.ddd.web.domain.DomainEventDispatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SyncDomainEventDispatcher implements DomainEventDispatcher {

    @Autowired
    private List<DomainEventListener> listeners;

    @Override
    public void dispatchNow(EventQueue eventQueue) {
        eventQueue.queue().forEach(event -> {
                    listeners.forEach(listener -> {
                        listener.onEvent(event);
                    });
                }

        );
    }
}
